package com.fintech.common.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Value;

import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * solr工具类
 * 
 * @author tux
 *
 */
public class SolrUtils {
	private static SolrUtils solrUtils = null;

	private static String solrUrl;
	
	private static HttpSolrClient httpSolrClient = null;

	
	public String getSolrUrl() {
		return solrUrl;
	}

	@Value("${solr.url}")
	public void setSolrUrl(String solrUrl) {
		SolrUtils.solrUrl = solrUrl;
	}

	public static HttpSolrClient contect() {
		getInstance();
		return httpSolrClient;
	}

	public static SolrUtils getInstance() {
		if (solrUtils == null) {
			init();
		}
		return solrUtils;
	}
	
	public static SolrUtils getInstance(String solrUrl) {
		if (solrUtils == null) {
			init(solrUrl);
		}
		return solrUtils;
	} 

	private static void init() {
		try {
			String url = StringUtils.trimToEmpty(solrUrl);
			httpSolrClient = new HttpSolrClient(url);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("初始化solr失败", e);
		}
	}
	
	private static void init(String solrUrl) {
		try {
			String url = StringUtils.trimToEmpty(solrUrl);
			// String url = "http://10.0.15.42:32002/solr/fx_check_core/";
			httpSolrClient = new HttpSolrClient(url);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("初始化solr失败", e);
		}
	}

	/**
	 * 通过SOLR获取数据
	 * 
	 * @param queryField
	 *            查询字段
	 * @param queryFieldVal
	 *            查询字段对应的值
	 * @param rtnField
	 *            返回的字段
	 * @return JSON结果
	 */
	public static String querySimpleRtnJson(String queryField, String queryFieldVal, String rtnField) {
		List<String> rtnList = new ArrayList<String>(1);
		rtnList.add(rtnField);
		return querySimpleRtnJson(queryField, queryFieldVal, rtnList);
	}

	/**
	 * 通过SOLR获取数据
	 * 
	 * @param queryField
	 *            查询字段
	 * @param queryFieldVal
	 *            查询字段对应的值
	 * @param rtnList
	 *            返回的字段名集合
	 * @return JSON结果
	 */
	public static String querySimpleRtnJson(String queryField, String queryFieldVal, List<String> rtnList) {
		if (StringUtils.isEmpty(queryField) || StringUtils.isEmpty(queryFieldVal) || rtnList == null
				|| rtnList.size() <= 0) {
			return "[]";
		}
		try {
			SolrQuery query = new SolrQuery();
			query.setQuery(queryField + ":" + queryFieldVal);
			query.setRows(Integer.MAX_VALUE);
			query.set("wt", "json");
			QueryResponse response = httpSolrClient.query(query);
			SolrDocumentList solrDocumentList = response.getResults();
			JSONArray ary = new JSONArray(); // 返回的JSON数组
			// 遍历列表
			for (SolrDocument doc : solrDocumentList) {
				JSONObject obj = new JSONObject();
				for (String rtnFieldName : rtnList) {
					obj.put(rtnFieldName, doc.get(rtnFieldName));
				}
				ary.add(obj);
			}
			return ary.toString();
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("简单查询失败", e);
		}
		return null;
	}

	public static void main(String[] args) {
		getInstance();
		System.out.println(querySimpleRtnJson("company_address", "横沥镇", "company_address"));
	}
}
